package it.rockliveitalia;

import it.rockliveitalia.database.EventDbHelper;

import java.util.ArrayList;
import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class StringListActivity extends Activity {

	private int mode;
	private ListView listView = null;
	private List<String> stringList = new ArrayList<String>();
	private Context context;
	private String string;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_string_list);
		mode = getIntent().getIntExtra("mode", 0);
		context = getApplicationContext();
		listView = (ListView) findViewById(R.id.string_list_view);
		EventDbHelper helper = new EventDbHelper(this);
		Cursor c;
		if (mode == 0) {
			c = helper.getBands();
			stringList = helper.cursorToStrings(c);
			setTitle(R.string.all_bands);
		} else {
			c = helper.getVenues();
			stringList = helper.cursorToStrings(c);
			setTitle(R.string.all_venues);
		}
		helper.close();
		CustomAdapterOptimize adapter = new CustomAdapterOptimize(this,
				R.layout.string_list_element, stringList);
		listView.setAdapter(adapter);

		OnItemClickListener clickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				string = (String) adapter.getItemAtPosition(position);
				final List<String> ids;
				if (mode == 0) {
					ids = bandFilter(string);
				} else {
					ids = venueFilter(string);
				}
				AlertDialog.Builder alertb = new AlertDialog.Builder(
						StringListActivity.this);
				alertb.setTitle("Mostra Eventi");
				alertb.setMessage("Scegli se visualizzare gli eventi associati sulla mappa o in una lista.");
				alertb.setNegativeButton("Mappa",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								Intent intent = new Intent(context,
										MapFilterActivity.class);
								intent.putStringArrayListExtra("ids",
										(ArrayList<String>) ids);
								intent.putExtra("filter", string);
								startActivity(intent);
							}
						});
				alertb.setNeutralButton("Lista",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								List<String> ids;

								if (mode == 0) {
									ids = bandFilter(string);
								} else {
									ids = venueFilter(string);

								}
								Intent intent = new Intent(context,
										EventListActivity.class);
								intent.putStringArrayListExtra("ids",
										(ArrayList<String>) ids);
								intent.putExtra("filter", string);
								startActivity(intent);
							}
						});
				alertb.setIcon(R.drawable.ic_content_event);
				AlertDialog alert = alertb.create();
				alert.show();
			}
		};
		listView.setOnItemClickListener(clickListener);
	}

	private List<String> bandFilter(String s) {
		List<String> result = new ArrayList<String>();
		EventDbHelper helper = new EventDbHelper(this);
		Cursor c = helper.getIDsFromBandName(s);
		result = helper.cursorToStrings(c);
		helper.close();
		return result;
	}

	private List<String> venueFilter(String s) {
		List<String> result = new ArrayList<String>();
		EventDbHelper helper = new EventDbHelper(this);
		Cursor c = helper.getIDsFromVenueName(s);
		result = helper.cursorToStrings(c);
		helper.close();
		return result;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	public class CustomAdapterOptimize extends ArrayAdapter<String> {

		public CustomAdapterOptimize(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return getViewOptimize(position, convertView, parent);
		}

		public View getViewOptimize(int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.string_list_element,
						null);
				viewHolder = new ViewHolder();
				viewHolder.string = (TextView) convertView
						.findViewById(R.id.element_string);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			String string = getItem(position);
			viewHolder.string.setText(string);

			return convertView;
		}

		public class ViewHolder {
			public TextView string;
		}
	}
	
	@Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance(this).activityStart(this);
	  }

	@Override
	protected void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this);
	}
}
